package com.stock.demo.framework.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.stock.demo.R;

/**
 * Created by XuZhaoyu on 2017/5/22.
 */

public class BottomBar extends LinearLayout {

    private RadioButton mRbItem0;
    private RadioButton mRbItem1;
    private RadioButton mRbItem2;
    private RadioButton mRbItem3;
    private RadioGroup mRadioGroup;
    private View mView;

    public BottomBar(Context context) {
        this(context, null);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mView = LayoutInflater.from(context).inflate(R.layout.view_bottom_bar, this, true);

        mRadioGroup = (RadioGroup) mView.findViewById(R.id.rg_bottom_bar);
        mRbItem0 = (RadioButton) mView.findViewById(R.id.rb_bottom_bar_item_0);
        mRbItem1 = (RadioButton) mView.findViewById(R.id.rb_bottom_bar_item_1);
        mRbItem2 = (RadioButton) mView.findViewById(R.id.rb_bottom_bar_item_2);
        mRbItem3 = (RadioButton) mView.findViewById(R.id.rb_bottom_bar_item_3);

        mRbItem0.setText(getResources().getTextArray(R.array.bottom_bar)[0]);
        mRbItem1.setText(getResources().getTextArray(R.array.bottom_bar)[1]);
        mRbItem2.setText(getResources().getTextArray(R.array.bottom_bar)[2]);
        mRbItem3.setText(getResources().getTextArray(R.array.bottom_bar)[3]);

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeGlobalOnLayoutListener(this);

                int height = (int) (getHeight() * 0.5);
                Drawable drawableHome = getResources().getDrawable(R.drawable.selector_bottom_bar_item_0);
                drawableHome.setBounds(0, 0, height, height);
                mRbItem0.setCompoundDrawables(null, drawableHome, null, null);

                Drawable drawableMarket = getResources().getDrawable(R.drawable.selector_bottom_bar_item_1);
                drawableMarket.setBounds(0, 0, height, height);
                mRbItem1.setCompoundDrawables(null, drawableMarket, null, null);

                Drawable drawableSelfSelect = getResources().getDrawable(R.drawable.selector_bottom_bar_item_2);
                drawableSelfSelect.setBounds(0, 0, height, height);
                mRbItem2.setCompoundDrawables(null, drawableSelfSelect, null, null);

                Drawable drawableTrade = getResources().getDrawable(R.drawable.selector_bottom_bar_item_3);
                drawableTrade.setBounds(0, 0, height, height);
                mRbItem3.setCompoundDrawables(null, drawableTrade, null, null);

            }

        });

        /**
         * 通过接口回调，将RadioGroup的选中监听暴露
         */
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChange(group, checkedId);
                }
            }
        });

    }

    public interface OnCheckedChangeListener {
        void onCheckedChange(RadioGroup group, int checkedId);
    }

    private OnCheckedChangeListener mOnCheckedChangeListener;


    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    public void setCheck(int position) {
        mRbItem0.setChecked(false);
        mRbItem1.setChecked(false);
        mRbItem2.setChecked(false);
        mRbItem3.setChecked(false);
        switch (position) {
            case 0:
                mRbItem0.setChecked(true);
                break;
            case 1:
                mRbItem1.setChecked(true);
                break;
            case 2:
                mRbItem2.setChecked(true);
                break;
            case 3:
                mRbItem3.setChecked(true);
                break;
        }
    }
}
